<%@ page import="POJO.*" %>
<%@ page import="DB.*" %>


<%
            boolean ok = true;
            UserManager db = new UserManager(DBInfo.getConnection());
            User u = (User) session.getAttribute("user");
            u.GetInfo();
            if (request.getParameter("page") == null || u == null) {
                ok = false;
%>

<h2 class="nice">Unauthorized entrance.</h2>

<%  } else if (!db.isConnected()) {
    ok = false;
    out.print("<h2 class=\"nice\">No connection with the database.</h2>");
} else if (request.getParameter("user") != null && !request.getParameter("user").matches("undefined") &&
        !(u.username != null && u.username.compareTo(request.getParameter("user")) == 0)) { //profiel van een andere gebruiker bekijken

    String other = request.getParameter("user");
    boolean exists = db.usernameExists(other);
    if (!exists) {
        ok = false;
        out.print("<h2 class=\"nice\">No user with username " + other + " in the database.</h2>");
    } else {
        if (db.showInfo(other)) {
            out.print("<h1 class=\"nice\">" + other + "</h1>");
            out.print("<div style=\"text-align:left;\"><table class=\"nice\" width = \"100%\"" +
                    " style=\"text-align:left;\">");
            out.print("<tr><td class=\"b\">Username:</td><td>&nbsp;&nbsp;&nbsp;" + other + "</td></tr>");
            out.print("<tr><td class=\"b\">Registration Date:</td><td>&nbsp;&nbsp;&nbsp;" + db.getRegistrationDate(other) + "</td></tr>");
            out.print("<tr><td class=\"b\">Full Name:</td><td>&nbsp;&nbsp;&nbsp;" + db.getFullName(other) + "</td></tr>");
            out.print("<tr><td class=\"b\">Email:</td><td>&nbsp;&nbsp;&nbsp;" + db.getEmail(other) + "</td></tr>");
            out.print("</table></div>");
        } else {
            out.print("<div><h2 class=\"nice\">" + other + " decided not to share his personal info.</h2></div>");
        }
    }
} else { //eigen profiel bekijken + mogelijkheid tot editen

    if (u.username == null) {
        ok = false;
        out.print("<div><h2 class=\"nice\">You need to log in before you can view/edit your personal information.</h2></div>");
    } else {
        if (request.getParameter("edit") != null && request.getParameter("edit").compareTo("true") == 0 && request.getMethod().equals("POST")) //nieuwe waarden zijn verstuurd
        {
            String newFullName = request.getParameter("fullName");
            String newEmail = request.getParameter("email");
            boolean showInfo = request.getParameterValues("info") != null && request.getParameterValues("info").length == 1;
            String pw = request.getParameter("password");

            db.changeEmail(u.username, newEmail);
            db.changeFullName(u.username, newFullName);
            db.changeShowInfo(u.username, showInfo);
            if (pw.compareTo("") != 0) {
                db.changePassword(u.username, pw);
            }
        }

        boolean show = db.showInfo(u.username);
        String pw = db.getPassword(u.username);

        out.print("<script type=\"text/javascript\" src=\"changeProfile.js\"></script>");

        out.print("<div style=\"text-align:left;\"><form action=\"index.jsp?page=profile&amp;edit=true\" method=\"post\" onsubmit =\"return checkform(this, 'false')\" ><table class=\"nice\" width = \"100%\" style=\"text-align:left;\">");
        out.print("<tr><td class=\"b\">Username:</td><td>&nbsp;&nbsp;&nbsp;" + u.username + "</td></tr>");
        out.print("<tr><td class=\"b\">Registration Date:</td><td>&nbsp;&nbsp;&nbsp;" + db.getRegistrationDate(u.username) + "</td></tr>");
        out.print("<tr><td class=\"b\">Full Name:</td><td>&nbsp;&nbsp;&nbsp;<input size=\"50\" type=\"text\" name=\"fullName\" value=\"" + db.getFullName(u.username) + "\"/></td></tr>");
        out.print("<tr><td class=\"b\">Email:</td><td>&nbsp;&nbsp;&nbsp;<input size=\"50\" type=\"text\" name=\"email\" value=\"" + db.getEmail(u.username) + "\"/></td></tr>");
        if (!show) {
            out.print("<tr><td class=\"b\">Make this information public:</td><td>&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"info\" value=\"info\"/></td></tr>");
        } else {
            out.print("<tr><td class=\"b\">Make this information public:</td><td>&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"info\" value=\"info\" checked=\"checked\"/></td></tr>");
        }
        out.print("<tr><td class=\"b\">New Password:</td><td valign=\"top\">&nbsp;&nbsp;&nbsp;<input size=\"50\" type=\"password\" name=\"password\" value=\"\"/></td></tr>");
        out.print("<tr><td class=\"b\">Repeat New Password:</td><td>&nbsp;&nbsp;&nbsp;<input size=\"50\" type=\"password\" name=\"password2\" value=\"\"/><br /><span style=\"font-size:9px; font-weight: normal; color: #666666;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(leave fields empty for keeping your current password)</span></td></tr>");
        out.print("<tr><td colspan=\"2\" align=\"center\"><br /><br /><input type=\"submit\" value=\"Change Profile\" /></td></tr></table></form></div>");
    }
}
%>

<%
            if (ok && request.getParameter("user") != null && !(u.username != null && u.username.compareTo(request.getParameter("user")) == 0)) {
                String other = request.getParameter("user");

                TripManager t = new TripManager(db.conn);
                int[] ids = t.getTripsOwnedBy(other);
                if (ids != null && ids.length > 0) {
                    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td class=\"b\">Trips made by " + other + ":</td></tr>");

                    for (int i = 0; i < ids.length; ++i) {
                        out.print("<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=trip&amp;tripid=" + ids[i] + "\">" + t.getTripName(ids[i]) + "</a></td></tr>");
                    }
                    out.print("</table></div>");
                }

                RelationManager r = new RelationManager(db.conn);
                String[] friendIds = r.getFriends(other);
                if (friendIds != null && friendIds.length > 0) {
                    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td class=\"b\">Friends of " + other + ":</td></tr>");
                    for (int i = 0; friendIds != null && i < friendIds.length; ++i) {
                        out.print("<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + friendIds[i] + "\">" + friendIds[i] + "</a></td></tr>");
                    }
                    out.print("</table></div>");
                }

                String[] famIds = r.getFamily(other);
                if (famIds != null && famIds.length > 0) {
                    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td class=\"b\">Family of " + other + ":</td></tr>");
                    for (int i = 0; famIds != null && i < famIds.length; ++i) {
                        out.print("<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + famIds[i] + "\">" + famIds[i] + "</a></td></tr>");
                    }
                    out.print("</table></div>");
                }

                if (u != null && u.username != null) {
                    if (request.getParameter("user") != null && request.getParameter("requestRelation") != null && request.getParameter("requestRelation").compareTo("true") == 0) {
                        if (request.getParameter("rel") != null && request.getParameter("rel").compareTo("friend") == 0) {
                            r.makeRelationRequest(u.username, request.getParameter("user"), "FRIEND");
                        } else if (request.getParameter("rel") != null && request.getParameter("rel").compareTo("family") == 0) {
                            r.makeRelationRequest(u.username, request.getParameter("user"), "FAMILY");
                        }
                    }

                    String rel = r.getAcceptedRelationBetween(other, u.username);
                    if (rel != null && rel.compareTo("FRIEND") == 0) {
                        out.print("<div class=\"nice\"><div class=\"b\">You're a friend of " + other + ".</div></div>");
                    } else if (rel != null && rel.compareTo("FAMILY") == 0) {
                        out.print("<div class=\"nice\"><div class=\"b\">You're family of " + other + ".</div></div>");
                    } else {
                        rel = r.getPendingRelationBetweenAskedByUser1(other, u.username);
                        if (rel != null && rel.compareTo("FRIEND") == 0) {
                            out.print("<div class=\"nice\"><div class=\"b\">" + other + " asked you to be his friend.</div></div>");
                        } else if (rel != null && rel.compareTo("FAMILY") == 0) {
                            out.print("<div class=\"nice\"><div class=\"b\">" + other + " asked you to be family.</div></div>");
                        } else {
                            rel = r.getPendingRelationBetweenAskedByUser2(other, u.username);

                            if (rel != null && rel.compareTo("FRIEND") == 0) {
                                out.print("<div class=\"nice\"><div class=\"b\">You asked " + other + " to be your friend.</div></div>");
                            } else if (rel != null && rel.compareTo("FAMILY") == 0) {
                                out.print("<div class=\"nice\"><div class=\"b\">You asked " + other + " to be family.</div></div>");
                            } else {
                                out.print("<div class=\"nice\"><form action=\"index.jsp?page=profile&amp;user=" + other + "&amp;requestRelation=true\" method=\"post\"><div class=\"b\">" +
                                        "<input type=\"radio\" name=\"rel\" value=\"friend\"/>&nbsp;&nbsp;You are a friend of " + other + ".<br/>" +
                                        "<input type=\"radio\" name=\"rel\" value=\"family\"/>&nbsp;&nbsp;You are family of " + other + ".<br/><br/>" +
                                        "<div style=\"text-align:center; width:100%;\"><input type=\"submit\" value=\"Ask for confirmation\" /></div></div></form></div>");
                            }
                        }
                    }
                }

            } else if (ok) {
                TripManager t = new TripManager(db.conn);
                if (request.getParameter("tripViewed") != null) //nieuwe waarden zijn verstuurd
                {
                    int id = Integer.parseInt(request.getParameter("tripViewed"));
                    t.viewed(id, u.username);
                }
                if (request.getParameter("tripRemoval") != null) //nieuwe waarden zijn verstuurd
                {
                    int id = Integer.parseInt(request.getParameter("tripRemoval"));
                    t.removeOwnership(id, u.username);
                }

                int[] ids = t.getTripsOwnedBy(u.username);
                out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td class=\"b\">Your trips:</td></tr>");

                if (ids != null && ids.length > 0) {
                    for (int i = 0; i < ids.length; ++i) {
                        if (t.getOwnershipRequestsByTripID(ids[i]) != null && t.getOwnershipRequestsByTripID(ids[i]).length > 0) {
                            out.print("<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=trip&amp;tripid=" + ids[i] + "\">" + t.getTripName(ids[i]) + "</a>  (ownership requests available)</td></tr>");
                        } else {
                            out.print("<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=trip&amp;tripid=" + ids[i] + "\">" + t.getTripName(ids[i]) + "</a></td></tr>");
                        }
                    }

                }
                out.print("<tr><td><br />&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=addTrip\">Add a new trip</a></td></tr>");
                out.print("</table></div>");

                ids = t.getPendingOwnershipRequests(u.username);
                if (ids != null && ids.length != 0) {
                    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td colspan=\"2\" class=\"b\">Your pending ownership requests:</td></tr>");
                    for (int i = 0; ids != null && i < ids.length; ++i) {
                        boolean accepted = t.isAccepted(ids[i], u.username);
                        boolean processed = t.isProcessed(ids[i], u.username);

                        if (!processed) {
                            out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=trip&amp;tripid=" + ids[i] + "\">" + t.getTripName(ids[i]) + "</a>: not yet confirmed</td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;tripRemoval=" + ids[i] + "\">[X]</a></td></tr>");
                        } else if (!accepted) {
                            out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=trip&amp;tripid=" + ids[i] + "\">" + t.getTripName(ids[i]) + "</a>: rejected</td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;tripRemoval=" + ids[i] + "\">[X]</a></td></tr>");
                        } else {
                            out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=trip&amp;tripid=" + ids[i] + "\">" + t.getTripName(ids[i]) + "</a>: accepted</td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;tripRemoval=" + ids[i] + "\">[X]</a>&nbsp;<a class = \"nice\" href=\"index.jsp?page=profile&amp;tripViewed=" + ids[i] + "\">[V]</a></td></tr>");
                        }
                    }
                    out.print("</table></div>");
                }


                RelationManager r = new RelationManager(db.conn);

                if (request.getParameter("friendRequests") != null && request.getParameter("friendRequests").compareTo("true") == 0 && request.getMethod().equals("POST")) //nieuwe waarden zijn verstuurd
                {
                    String[] friendRequests = r.getFriendRequests(u.username);
                    for (int i = 0; friendRequests != null && i < friendRequests.length; ++i) {
                        if (request.getParameter(friendRequests[i]) != null && request.getParameter(friendRequests[i]).compareTo("accept") == 0) {
                            r.acceptRequest(friendRequests[i], u.username);
                        } else if (request.getParameter(friendRequests[i]) != null && request.getParameter(friendRequests[i]).compareTo("reject") == 0) {
                            r.declineRequest(friendRequests[i], u.username);
                        }
                    }
                }

                if (request.getParameter("familyRequests") != null && request.getParameter("familyRequests").compareTo("true") == 0 && request.getMethod().equals("POST")) //nieuwe waarden zijn verstuurd
                {
                    String[] friendRequests = r.getFamilyRequests(u.username);
                    for (int i = 0; friendRequests != null && i < friendRequests.length; ++i) {
                        if (request.getParameter(friendRequests[i]) != null && request.getParameter(friendRequests[i]).compareTo("accept") == 0) {
                            r.acceptRequest(friendRequests[i], u.username);
                        } else if (request.getParameter(friendRequests[i]) != null && request.getParameter(friendRequests[i]).compareTo("reject") == 0) {
                            r.declineRequest(friendRequests[i], u.username);
                        }
                    }
                }

                if (request.getParameter("friendRemoval") != null) //nieuwe waarden zijn verstuurd
                {
                    String friendId = request.getParameter("friendRemoval");
                    r.removeRelation(friendId, u.username);
                }

                if (request.getParameter("familyRemoval") != null) //nieuwe waarden zijn verstuurd
                {
                    String familyId = request.getParameter("familyRemoval");
                    r.removeRelation(familyId, u.username);
                }

                String[] friendIds = r.getFriends(u.username);
                if (friendIds != null && friendIds.length > 0) {
                    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td colspan=\"2\" class=\"b\">Your friends:</td></tr>");
                    for (int i = 0; i < friendIds.length; ++i) {
                        out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + friendIds[i] + "\">" + friendIds[i] + "</a></td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;friendRemoval=" + friendIds[i] + "\">[X]</a></td></tr>");
                    }
                    out.print("</table></div>");
                }

                String[] friendRequests = r.getFriendRequests(u.username);
                if (friendRequests != null && friendRequests.length != 0) {
                    out.print("<div><form action=\"index.jsp?page=profile&amp;friendRequests=true\" method=\"post\"><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td colspan=\"2\" class=\"b\">Your friend requests:</td></tr>");
                    for (int i = 0; i < friendRequests.length; ++i) {
                        out.print("<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + friendRequests[i] + "\">" + friendRequests[i] + "</a></td>" +
                                "<td><input type=\"radio\" name=\"" + friendRequests[i] + "\" value=\"accept\"/> Accept <input type=\"radio\" name=\"" + friendRequests[i] + "\" value=\"reject\"/> Reject</td></tr>");
                    }
                    out.print("<tr><td colspan=\"2\" align=\"center\"><br /><br /><input type=\"submit\" value=\"Process requests\" /></td></tr></table></form></div>");
                }

                if (request.getParameter("friendViewed") != null) //nieuwe waarden zijn verstuurd
                {
                    String friendIdo = request.getParameter("friendViewed");
                    r.viewed(u.username, friendIdo);
                }

                String[] friendPendingRequests = r.getPendingFriendRequests(u.username);
                if (friendPendingRequests != null && friendPendingRequests.length != 0) {
                    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td colspan=\"2\" class=\"b\">Your pending friend requests:</td></tr>");
                    for (int i = 0; i < friendPendingRequests.length; ++i) {
                        boolean accepted = r.isAccepted(u.username, friendPendingRequests[i]);
                        boolean processed = r.isProcessed(u.username, friendPendingRequests[i]);

                        if (!processed) {
                            out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + friendPendingRequests[i] + "\">" + friendPendingRequests[i] + "</a>: not yet confirmed</td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;friendRemoval=" + friendPendingRequests[i] + "\">[X]</a></td></tr>");
                        } else if (!accepted) {
                            out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + friendPendingRequests[i] + "\">" + friendPendingRequests[i] + "</a>: rejected</td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;friendRemoval=" + friendPendingRequests[i] + "\">[X]</a></td></tr>");
                        } else {
                            out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + friendPendingRequests[i] + "\">" + friendPendingRequests[i] + "</a>: accepted</td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;friendRemoval=" + friendPendingRequests[i] + "\">[X]</a>&nbsp;<a class = \"nice\" href=\"index.jsp?page=profile&amp;friendViewed=" + friendPendingRequests[i] + "\">[V]</a></td></tr>");
                        }
                    }
                    out.print("</table></div>");
                }

                String[] familyIds = r.getFamily(u.username);
                if (familyIds != null && familyIds.length > 0) {
                    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td colspan=\"2\" class=\"b\">Your family:</td></tr>");
                    for (int i = 0; i < familyIds.length; ++i) {
                        out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + familyIds[i] + "\">" + familyIds[i] + "</a></td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;familyRemoval=" + familyIds[i] + "\">[X]</a></td></tr>");
                    }
                    out.print("</table></div>");
                }

                friendRequests = r.getFamilyRequests(u.username);
                if (friendRequests != null && friendRequests.length != 0) {
                    out.print("<div><form action=\"index.jsp?page=profile&amp;familyRequests=true\" method=\"post\"><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td colspan=\"2\" class=\"b\">Your family requests:</td></tr>");
                    for (int i = 0; friendRequests != null && i < friendRequests.length; ++i) {
                        out.print("<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + friendRequests[i] + "\">" + friendRequests[i] + "</a></td>" +
                                "<td><input type=\"radio\" name=\"" + friendRequests[i] + "\" value=\"accept\"/> Accept <input type=\"radio\" name=\"" + friendRequests[i] + "\" value=\"reject\"/> Reject</td></tr>");
                    }
                    out.print("<tr><td colspan=\"2\" align=\"center\"><br /><br /><input type=\"submit\" value=\"Process requests\" /></td></tr></table></form></div>");
                }

                if (request.getParameter("familyViewed") != null) //nieuwe waarden zijn verstuurd
                {
                    String friendIdo = request.getParameter("familyViewed");
                    r.viewed(u.username, friendIdo);
                }

                String[] familyPendingRequests = r.getPendingFamilyRequests(u.username);
                if (familyPendingRequests != null && familyPendingRequests.length != 0) {
                    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td colspan=\"2\" class=\"b\">Your pending family requests:</td></tr>");
                    for (int i = 0; familyPendingRequests != null && i < familyPendingRequests.length; ++i) {
                        boolean accepted = r.isAccepted(u.username, familyPendingRequests[i]);
                        boolean processed = r.isProcessed(u.username, familyPendingRequests[i]);

                        if (!processed) {
                            out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + familyPendingRequests[i] + "\">" + familyPendingRequests[i] + "</a>: not yet confirmed</td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;friendRemoval=" + familyPendingRequests[i] + "\">[X]</a></td></tr>");
                        } else if (!accepted) {
                            out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + familyPendingRequests[i] + "\">" + familyPendingRequests[i] + "</a>: rejected</td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;friendRemoval=" + familyPendingRequests[i] + "\">[X]</a></td></tr>");
                        } else {
                            out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + familyPendingRequests[i] + "\">" + familyPendingRequests[i] + "</a>: accepted</td><td style=\"width: 8em;\"><a class = \"nice\" href=\"index.jsp?page=profile&amp;friendRemoval=" + familyPendingRequests[i] + "\">[X]</a>&nbsp;<a class = \"nice\" href=\"index.jsp?page=profile&amp;familyViewed=" + familyPendingRequests[i] + "\">[V]</a></td></tr>");
                        }
                    }
                    out.print("</table></div>");
                }

                Foaf f = new Foaf(u);
                String[] foafIds = f.getFriendsOfFriends(2);
                if (foafIds != null && foafIds.length > 0) {
                    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td colspan=\"2\" class=\"b\">Friends/family of friends/family:</td></tr>");
                    for (int i = 0; i < foafIds.length; ++i) {
                        out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + foafIds[i] + "\">" + foafIds[i] + "</a></td></tr>");
                    }
                    out.print("</table></div>");
                }
%>

<script type="text/javascript">
    var xmlHttp;
    
    function showFOAF()
    {        
        xmlHttp=GetXmlHttpObject();
        xmlHttp.onreadystatechange = function()
        {
            if(xmlHttp.readyState == 4)
            {                
                document.getElementById("addFOAF").innerHTML = xmlHttp.responseText;
            }
        }
        xmlHttp.open("POST", "foaf?user=<% out.print(u.username);%>", true);
        xmlHttp.send("");
    }
    
    function GetXmlHttpObject()
    {
        var xmlHttp=null;
        try
        {
            xmlHttp=new XMLHttpRequest();
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }
</script>

<%
                out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td class=\"b\" style=\"text-align:left;\"><span class=\"anice\" onclick=\"showFOAF();\">Generate your FOAF file</span></td></tr><tr><td><div id=\"addFOAF\"></div></td></tr></table></div>");

            }

%>

